<template>
	<view class="login">
		<view class="title">
			亲，欢迎登录
		</view>
		<view class="text">
			没有账号？
			<navigator class="register" url="/pages/register/register">立即注册</navigator>
		</view>
		<view class="loginForm">
			<form @submit="submit">
				<view class="user">
					<input type="text" name="user" placeholder-class="placeholder" value="" placeholder="请输入账号" />
				</view>
				<view class="pass">
					<input type="text" name="pass" placeholder-class="placeholder" value="" placeholder="请输入密码" />
				</view>
				<button type="warn" class="submit" form-type="submit">确认</button>
			</form>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					user: "",
					pass: ""
				}
			}
		},
		methods: {
			submit(e) {
				this.form.user = e.detail.value.user;
				this.form.pass = e.detail.value.pass;
				this.request();
			},
			request() {
				uni.request({
					url: 'http://api_dev.wanxikeji.cn/api/login', 
					method: "POST",
					data: {
						name: this.form.user,
						pw: this.form.pass,
					},
					success: (res) => {
						console.log(res)
						if (res.data.code == 2000) {
							// 存储token
							uni.setStorageSync('token', res.data.data.token);
							// 跳转页面
							uni.switchTab({
								url: '/pages/mine/mine',
								// success() {
								// 	let page = getCurrentPages().pop(); //跳转页面成功之后                                        
								// 	console.log(getCurrentPages(), page);
								// 	if (!page) return;
								// 	page.onLoad(); //如果页面存在，则重新刷新页面
								// }
							});
						} else {
							console.log(res.data.msg)
						}
					}
				});
			}
		}
	}
</script>

<style>
	.login {
		padding: 20rpx;
		margin-top: 100rpx;
	}

	.title {
		font-size: 50rpx;
	}

	.text {
		color: #999;
		margin-top: 10rpx;
		font-size: 30rpx;
	}

	.register {
		display: inline-block;
		color: #f00;
	}

	.loginForm {
		margin-top: 50rpx;
	}

	.loginForm view {
		margin-top: 30rpx;
		border-bottom: 1rpx solid #ccc;
	}

	.placeholder {
		font-size: 30rpx;
		color: #ccc;
	}

	.submit {
		margin-top: 100rpx;
	}
</style>
